﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>

MainWindow::MainWindow(QMainWindow *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->resize(850,600);

    m_ipLabel = new QLabel("IP:",this);
    m_ipLabel->setGeometry(30,30,80,30);
    m_ipLineEdit = new QLineEdit(this);
    m_ipLineEdit->setGeometry(120,30,160,30);
    m_ipLineEdit->setText("127.0.0.1");

    m_portLabel = new QLabel("Port:",this);
    m_portLabel->setGeometry(310,30,40,30);
    m_portLineEdit = new QLineEdit(this);
    m_portLineEdit->setGeometry(360,30,80,30);
    m_portLineEdit->setText("21");

    m_userLabel = new QLabel("User:",this);
    m_userLabel->setGeometry(30,70,80,30);
    m_userLineEdit = new QLineEdit(this);
    m_userLineEdit->setGeometry(120,70,160,30);
    m_userLineEdit->setText("admin");

    m_passwordLabel = new QLabel("PassWord:",this);
    m_passwordLabel->setGeometry(310,70,80,30);
    m_passwordLineEdit = new QLineEdit(this);
    m_passwordLineEdit->setGeometry(400,70,160,30);
    m_passwordLineEdit->setText("admin");

    m_openDirBtn = new QPushButton("Choose Update Path",this);
    m_openDirBtn->setGeometry(30,110,180,30);
    connect(m_openDirBtn,&QPushButton::clicked,this,&MainWindow::on_openFile);

    m_localUploadFilePathLineEdit = new QLineEdit(this);
    m_localUploadFilePathLineEdit->setGeometry(220,110,500,30);
    m_localUploadFilePathLineEdit->setText("C:/Users/finger/Desktop/FTPUp_DownDemo/upload");

    QLabel *label_1 = new QLabel("Local Root Path:",this);
    label_1->setGeometry(30,150,180,30);
    m_localPathLineEdit = new  QLineEdit(this);
    m_localPathLineEdit->setGeometry(220,150,500,30);
    m_localPathLineEdit->setText("C:/Users/finger/Desktop/FTPUp_DownDemo/Down");


    QLabel *label_2 = new QLabel("FTP Root Path:",this);
    label_2->setGeometry(30,190,180,30);
    m_remoteFtpPathLineEdit = new  QLineEdit(this);
    m_remoteFtpPathLineEdit->setGeometry(220,190,500,30);
    m_remoteFtpPathLineEdit->setText("hello/");

    m_updownloadFileWork = FTPManager::getInstance();

    m_uploadBtn = new QPushButton("UpLoad",this);
    m_uploadBtn->setGeometry(30,230,80,30);
    connect(m_uploadBtn,&QPushButton::clicked,this,&MainWindow::on_upLoad);
    connect(this,&MainWindow::evt_beginFTPUpLoad,m_updownloadFileWork,&FTPManager::on_startFTPUpload);

    m_downloadBtn = new QPushButton("DownLoad",this);
    m_downloadBtn->setGeometry(120,230,80,30);
    connect(m_downloadBtn,&QPushButton::clicked,this,&MainWindow::on_downLoad);
    connect(this,&MainWindow::evt_beginFTPDownLoad,m_updownloadFileWork,&FTPManager::on_startFTPDownLoad);

    connect(m_updownloadFileWork,&FTPManager::evt_finishOneFileTransfer,this,&MainWindow::on_finishOneFileTransfer);

    m_setmd5fileRootBtn = new  QPushButton("choose file/dir to encode",this);
    m_setmd5fileRootBtn->setGeometry(30,270,260,30);
    connect(m_setmd5fileRootBtn,&QPushButton::clicked,this,&MainWindow::on_setEncodeFileRoot);

    m_md5fileRootEdit = new QLineEdit(this);
    m_md5fileRootEdit->setGeometry(300,270,500,30);

    m_fileEncodeManger = new  FileEncodeManager();
    m_createMd5fileRootBtn = new QPushButton("ENCODE",this);
    m_createMd5fileRootBtn->setGeometry(810,270,80,30);
    connect(m_createMd5fileRootBtn,&QPushButton::clicked,this,&MainWindow::on_beginFileEncode);
    connect(this,&MainWindow::evt_fileEncode,m_fileEncodeManger,&FileEncodeManager::on_fileEncode);
//    QLineEdit *m_md5fileRootEdit;
//    QPushButton *m_createMd5fileRootBtn;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_openFile()
{
    localRootPath = QFileDialog::getExistingDirectory(this, "选择文件","./");
    m_localUploadFilePathLineEdit->setText(localRootPath);
}

void MainWindow::on_upLoad()
{
    successfulFileNum = 0;
    QFileInfo *fileInfo = new QFileInfo(m_localUploadFilePathLineEdit->text());
    st_fileInforList.fileInfoList = new QList<QFileInfo>;
    if(!FileFunction::getAllFileInfoList(fileInfo->filePath(),st_fileInforList.fileInfoList))
    {
        qDebug()<<"需要上传的文件："<<fileInfo->filePath()<<"不存在!"<<endl;
    }
    else
    {
        st_fileInforList.localFileRootPath = localRootPath;
        st_fileInforList.remoteFtpFileRootPath = m_remoteFtpPathLineEdit->text();
        m_updownloadFileWork->setRemoteUrl(m_ipLineEdit->text());
        m_updownloadFileWork->setRemoteUrlPort(m_portLineEdit->text());
        m_updownloadFileWork->setUserNamePassWord(m_userLineEdit->text(),m_passwordLineEdit->text());
        m_updownloadFileWork->getFileList(st_fileInforList);
        ret  = emit evt_beginFTPUpLoad();
        showFTPStatus(ret);
        st_fileInforList.fileInfoList->clear();
    }
    successfulFileNum = 0;
}

void MainWindow::on_downLoad()
{
    successfulFileNum = 0;
    st_fileInforList.fileInfoList = new QList<QFileInfo>;
    if(!FileFunction::getAllFileInfoList(m_localUploadFilePathLineEdit->text(),st_fileInforList.fileInfoList))
    {
        qDebug()<<"需要上传的文件："<<m_localUploadFilePathLineEdit->text()<<"不存在!"<<endl;
    }
    else
    {
        st_fileInforList.localFileRootPath = m_localPathLineEdit->text();
        st_fileInforList.remoteFtpFileRootPath = m_remoteFtpPathLineEdit->text();
        m_updownloadFileWork->setRemoteUrl(m_ipLineEdit->text());
        m_updownloadFileWork->setRemoteUrlPort(m_portLineEdit->text());
        m_updownloadFileWork->setUserNamePassWord(m_userLineEdit->text(),m_passwordLineEdit->text());
        m_updownloadFileWork->getFileList(st_fileInforList);
        ret  = emit evt_beginFTPDownLoad();
        showFTPStatus(ret);
        st_fileInforList.fileInfoList->clear();
    }
}

void MainWindow::on_finishOneFileTransfer()
{
    successfulFileNum++;
}

void MainWindow::on_setEncodeFileRoot()
{
    md5EncodeFileRoot = QFileDialog::getExistingDirectory(this, "选择文件夹","./");
    m_md5fileRootEdit->setText(md5EncodeFileRoot);
}

void MainWindow::on_beginFileEncode()
{
    m_fileEncodeManger->setFileEncodePath(m_md5fileRootEdit->text());
    emit evt_fileEncode();
}

void MainWindow::showFTPStatus(int ret)
{
    qDebug()<<"FTP Transfer end :";
    if(ret == NULL_FILELIST)
        qDebug()<<"Transfer failed, transport list is empty!";
    else if(ret == ERROR_ON_TRANSMISSION)
        qDebug()<<"A error occurred during the transmission!"<<endl;
    else if(ret == SUCCESS_TRANSMISSION)
        qDebug()<<"Transfer success!"<<endl;
}

